package com.kly.smartbi.manager;

import org.redisson.api.RRateLimiter;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;

/**
 * @author admin
 * @date 2025/3/31
 */
@Component
public class RedisLimiterManager {

    @Resource
    private RedissonClient redissonClient;

    public void doRateLimit(String key) {
        RRateLimiter rateLimiter = redissonClient.getRateLimiter(key);

        rateLimiter.trySetRate(RateType.OVERALL, 10, Duration.ofSeconds(1));
        boolean canOp = rateLimiter.tryAcquire(1);
        if (!canOp) {
            throw new RuntimeException("请求过于频繁");
        }
    }
}
